<%@page import="model.environment.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<% 	PageViewInfo pvi = new PageViewInfo(); 
	pvi.setPageTitle("Administration - edit item");
	
	// Sending a 403 if the user isn't an admin
	if (!Login.isAdministrator(session)) {
		response.sendError(403);
	}
	
	int itemID;
	Item item = null;
	
	try {
		itemID = Integer.parseInt(request.getParameter("itemID"));
		item = Items.getItem(itemID);
		
		// Do we have an item with that id? Use the items name as the page's title.
		// If not, create a new item.
		if (item == null)
			itemID = -1;
		else
			pvi.setPageTitle("Administration - \"" + item.getItemName() + "\"");
	} catch (NumberFormatException e) {
		// itemID was faulty or non-existing. Create a new item.
		itemID = -1;
	}
	
	String submitButtonValue = "Edit item";
	
	// If we have a new item (item == null), create a new "empty" item.
	// This means that we can easily pre-fill-out the form,
	// for example "item.getItemName()" resolves to "" if item was == null,
	// but it resolves to the actual item name if item != null.
	if (item == null) {
		item = new Item(-1, "", "", 0, 0, "");
		submitButtonValue = "Add item";
	}
	%>
<%@include file="../parts/header.jsp" %>
	<div class="content">
		<%
		Object sessionMessage = session.getAttribute("pviMessage");
		if (sessionMessage == StaticInfo.Message.CREATE_ITEM_FAILED)
		{
			out.println("Could not create item.<br/><br/>");
			session.setAttribute("pviMessage", null);
		} else if (sessionMessage == StaticInfo.Message.EDIT_ITEM_FAILED) {
			out.println("Could not edit item.<br/><br/>");
			session.setAttribute("pviMessage", null);
		} else if (sessionMessage == StaticInfo.Message.EDIT_ITEM_OK) {
			out.println("Item was updated.<br/><br/>");
			session.setAttribute("pviMessage", null);
		} else if (sessionMessage == StaticInfo.Message.ADJUST_STOCK_FAILED) {
			out.println("Stock could not be updated.<br/><br/>");
			session.setAttribute("pviMessage", null);
		} else if (sessionMessage == StaticInfo.Message.VALIDATE_ITEM_DESCRIPTION_FAILED) {
			out.println("Invalid tags was removed from the itemDescription, only &lt;i&gt;, &lt;b&gt;, &lt;ul&gt;, &lt;li&gt; and &lt;span&gt; are allowed.");
			session.setAttribute("pviMessage", null);
		}
		%>
		<style>
			label {
				font: 20px bold;
			}
			input, textarea {
				line-height:24px;
				font: 20px;
			}
			form p {
				margin-bottom:10px;
			}
		</style>
		<script type="text/javascript">
		$(document).ready(function(){	
			$("#itemURLPreview").click(function(){
				var src = $("#url").val();
				$.modal('<iframe src="' + src + '" height="450" width="830" style="border:0">', {
				    containerCss:{
				        height:450,
				        padding:0,
				        width:830
				    },
				    overlayClose:true
				}); 
			});
		});	

		</script>
		<form action="<%= request.getContextPath() %>/editItem.do" method="post">
			<p>
				<label for="name">Item name:</label><br />
				<input type="text" name="name" id="name" value="<%= item.getItemName() %>"/>
			</p>
			<p>
				<label for="url">Item URL:</label><br/>
				<input type="text" name="url" id="url" value="<%= item.getItemURL() %>"/> <a style="margin-left:10px;" id="itemURLPreview" href="#">Preview</a>
			</p>
			<p>
				<label for="description">Item description:</label><br/>
				<textarea name="description" id="description" rows="4" cols="30"><%= item.getItemDescription() %></textarea>
			</p>
			<p>
				<label for="price">Item price:</label><br/>
				<input type="text" name="price" id="price" value="<%= item.getItemPrice() %>"/>
			</p>
			<p>
				<label for="stock">Item stock:</label><br/>
				<input type="text" name="stock" id="stock" value="<%= item.getItemStock() %>"/>
			</p>
				
			
			<input type="hidden" name="id" id="id" value="<%= itemID %>">
			<input type="submit" value="<%= submitButtonValue %>" style="padding:10px 20px;font:23px bold;">
		</form>
 	</div>
<%@include file="../parts/footer.jsp" %>